
import { sandbox } from '@/libs/util'
import selectOptions from '@/publicComponents/selectOptions/index'

export default {
  props: {
    mode: {
      type: String,
      default: ''
    },
  },
  components: { selectOptions },
  data (vm) {
    let opt = {
        btnLoading: false,
        rules: {},
        form: sandbox({})
      },
      tar = {}

    for (let key in opt) {
      if (!(key in vm)) {
        Object.assign(tar, { [key]: opt[key] })
      }
    }

    return tar
  },
  methods: {
    AddRow () {
      return Promise.resolve()
    },
    UpdateRow () {
      return Promise.resolve()
    },
    SetData(row){
      Object.assign(this.form, row)
    },
    // closeForm
    closeForm (isReload= '') {
      this.$emit('back',isReload)
      this.btnLoading = false
      this.$refs.groupForm && this.$refs.groupForm.resetFields()
      this.form.reset()
    },
    handleSubmit () {
      this.$refs.groupForm.validate(valid => {
        if (valid) {
          this.btnLoading = true
          ;(this.mode === 'add' ? this.AddRow() : this.UpdateRow())
            .then(() => {
              this.closeForm('reload')
            })
            .finally(() => {
              this.btnLoading = false
            })
        }
      })
    }
  }
}
